﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Configuration;

/// <summary>
/// Description of DB.
/// </summary>
public class DB
{
    //public static string ConnectionString = string.Format("Data Source={0};", System.Configuration.ConfigurationSettings.AppSettings["sqlite"]);
    public static string ConnectionString = string.Format("Data Source={0};", System.Environment.CurrentDirectory + "\\debug.s3db");

    public string NewId{
    	get{
            return Guid.NewGuid().ToString("N");
    	}
    }

    ~DB()
    {
        if (connection != null && connection.State != ConnectionState.Closed)
        {
            connection.Close();
        }
    }

    public override string ToString()
    {
        return ConnectionString;
        //return base.ToString();
    }

    protected DB()
    {
    }

    private static DB _instance;

    public static DB getInstance()
    {
        if (_instance == null)
        {
            _instance = new DB();
        }
        return _instance;
    }

    private IDbConnection connection;

    public IDbConnection Connection()
    {
        if (connection == null)
        {
            connection = new SQLiteConnection(ConnectionString);
        }
        if (connection.State != ConnectionState.Open)
            connection.Open();
        return connection;
    }

    public IDbCommand Command(string CommandText)
    {
        IDbConnection cn = Connection();
        if (cn.State != ConnectionState.Open)
            cn.Open();
        IDbCommand cm = cn.CreateCommand();
        cm.CommandText = CommandText;
        return cm;
    }

    public IDataReader Query(string SQL)
    {
        return Command(SQL).ExecuteReader();
    }

    public DataTable List(string SQL)
    {
        SQLiteDataAdapter da = new SQLiteDataAdapter(Command(SQL) as SQLiteCommand);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }

    public int Affected(string SQL)
    {
        return Command(SQL).ExecuteNonQuery();
    }

    public void Excute(string SQL)
    {
        Command(SQL).ExecuteNonQuery();
    }

    #region Unitl

    public string DB_Safe_Number(string value)
    {
        return value;
        //return string.Format("'{0}'}", value);
    }

    public string DB_Safe_Number(int value)
    {
        return value.ToString();
    }

    public string DB_Safe_Number(object value)
    {
        if (value == null || value == DBNull.Value)
            return "null";
        else
            return value.ToString();
    }

    public string DB_Safe_String(string value)
    {
        if (value == null)
            return "null";
        return string.Format("'{0}'", value);
    }

    public string DB_Safe_String(object value)
    {
        if (value == null || value == DBNull.Value)
        {
            return "null";
        }
        else
        {
            return string.Format("'{0}'", value);
        }
    }

    public string DB_Safe_DateTime(string value)
    {
        return string.Format("'{0}'", value);
    }

    public int DB_Parse_Number(object obj, int Default)
    {
        int Result;
        if (obj != null && obj != DBNull.Value && int.TryParse(obj.ToString(), out Result))
        {
            return Result;
        }
        return Default;
    }

    public int DB_Parse_Number(object value)
    {
        return DB_Parse_Number(value, 0);
    }

    public string DB_Parse_String(object value)
    {
        if (value != null && value != DBNull.Value)
            return value.ToString();
        return null;
    }

    #endregion
}